import { classicBeh } from '../classic-beh.js'
const mMgr = wx.getBackgroundAudioManager();
Component({

  behaviors: [ classicBeh ],

  properties: {
    src: String,
    title: String
  },
  /**
   * 组件的初始数据
   */
  data: {
    playing: false,
    pauseSrc: 'images/player@pause.png',
    playSrc: 'images/player@play.png'
  },

  attached() {
    this._recoverStatus()
    this._moniterSwitch()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onPlay() {
      if(this.data.playing) {
        this.setData({
          playing: false
        })
        mMgr.pause()
      } else {
        this.setData({
          playing: true
        })
        mMgr.title = this.properties.title
        mMgr.src = this.properties.src
      }
    },

    _recoverStatus() {
      if(mMgr.paused) {
        this.setData({
          playing: false
        })
        return
      }
      if(mMgr.src === this.properties.src) {
        this.setData({
          playing: true
        })
      }
    },

    _moniterSwitch() {
      mMgr.onPlay(() => {
        this._recoverStatus()
      })
      mMgr.onPause(() => {
        this._recoverStatus()
      })
      mMgr.onStop(() => {
        this._recoverStatus()
      })
      mMgr.onEnded(() => {
        this._recoverStatus()
      })
    }
  }
})
